package org.itboys.core.util;

import org.apache.commons.lang.StringUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author：贾琦磊
 * @Date：2018/10/30
 * @Description：mybatis查询参数封装，只封装分页参数和排序参数，其他参数请自行从request对象中获取
 */
public class MybatisParamUtil {
    //数据开始
    private static Integer ROW_START = 0;
    //默认每页大小
    private static Integer ROW_SIZE = 20;
    //默认排序字段
    private static String SORT_NAME ="id";
    //默认排序顺序
    private static String SORT_ORDER="DESC";
    /**起始行*/
    private Integer rowStart;
    /**查询数据行数*/
    private Integer rowSize;
    /**排序字段*/
    private String sortName;
    /**排序方向*/
    private String sortOrder;

    public MybatisParamUtil(HttpServletRequest request) {
        packageMysqlPageParam(request);
    }

    private void packageMysqlPageParam(HttpServletRequest request){

        //组装分页参数
        //	Integer rowStart =StringUtils.isNotEmpty(request.getParameter("offset"))?Integer.valueOf(request.getParameter("offset")):ROW_START;
        //	Integer rowSize =StringUtils.isNotEmpty(request.getParameter("limit"))?Integer.valueOf(request.getParameter("limit")):ROW_SIZE;

        Integer rowStart =ROW_START;
        Integer rowSize =ROW_SIZE;
        /*********************分页参数需要兼容bootstrap,easyUI，mobile（手机端）**/
	/* * bootstrap分页参数：offset(起始位置)，limit（条数）
 	* mobile（手机端）分页参数：pageNumber(页数),pageSize(条数)
	* easyUi分页参数：page(页数),rows(条数)**/
        //1，兼容bootstrap
        if(StringUtils.isNotEmpty(request.getParameter("offset"))&&
                StringUtils.isNotEmpty(request.getParameter("limit"))){
            rowStart=Integer.valueOf(request.getParameter("offset"));
            rowSize=Integer.valueOf(request.getParameter("limit"));
        }
        //2，兼容easyUI。asyUi分页参数：page(页数),rows(条数)
        if(StringUtils.isNotEmpty(request.getParameter("page"))&&
                StringUtils.isNotEmpty(request.getParameter("rows"))){
            Integer page=Integer.valueOf(request.getParameter("page"));
            Integer rows=Integer.valueOf(request.getParameter("rows"));
            rowStart=(page-1)*rows;
            rowSize=rows;
        }
        //3，兼容mobile（手机端）。分页参数：pageNumber(页数),pageSize(条数)
        if(StringUtils.isNotEmpty(request.getParameter("pageNumber"))&&
                StringUtils.isNotEmpty(request.getParameter("pageSize"))){
            Integer pageNumber=Integer.valueOf(request.getParameter("pageNumber"));
            Integer pageSize=Integer.valueOf(request.getParameter("pageSize"));
            rowStart=(pageNumber-1)*pageSize;
            rowSize=pageSize;
        }

        this.rowStart=rowStart;
        this.rowSize=rowSize;
        //组装排序参数
        String sort =StringUtils.isNotEmpty(request.getParameter("sort"))?request.getParameter("sort"):SORT_NAME;

        this.sortName=sort;
        String order =StringUtils.isNotEmpty(request.getParameter("order"))?request.getParameter("order"):SORT_ORDER;
        this.sortOrder=order;
    }
    public Integer getRowStart() {
        return rowStart;
    }

    public Integer getRowSize() {
        return rowSize;
    }

    public String getSortName() {
        return sortName;
    }

    public String getSortOrder() {
        return sortOrder;
    }
}
